package cn.wolfcode.p2p.mgrsite.controller;

import cn.wolfcode.p2p.base.domain.UserInfo;
import cn.wolfcode.p2p.base.excetion.DisplayableExcetion;
import cn.wolfcode.p2p.base.service.IAccountService;
import cn.wolfcode.p2p.base.service.IRealAuthService;
import cn.wolfcode.p2p.base.service.IUserInfoService;
import cn.wolfcode.p2p.base.util.AjaxResult;
import cn.wolfcode.p2p.base.util.Constants;
import cn.wolfcode.p2p.bussness.domain.BidRequest;
import cn.wolfcode.p2p.bussness.domain.BidRequestAuditHistory;
import cn.wolfcode.p2p.bussness.query.BidRequestQueryObject;
import cn.wolfcode.p2p.bussness.service.IBidRequestAuditHistoryService;
import cn.wolfcode.p2p.bussness.service.IBidRequestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

//后台借款申请审核
@Controller
public class BidRequestController {

    @Autowired
    private IUserInfoService userInfoService;
    @Autowired
    private IAccountService accountService;
    @Autowired
    private IBidRequestService bidRequestService;
    @Autowired
    private IRealAuthService realAuthService;
    @Autowired
    private IBidRequestAuditHistoryService bidRequestAuditHistoryService;
            ;

    @RequestMapping("/bidrequest_publishaudit_list")
    public String bidrequestPublishauditList(@ModelAttribute("qo") BidRequestQueryObject qo, Model model) {
        qo.setBidRequestState(Constants.BIDREQUEST_STATE_APPLY);
        model.addAttribute("pageInfo", bidRequestService.queryPage(qo));
        return "bidrequest/publish_audit";
    }

    //审核通过/退回功能
    @RequestMapping("/bidrequest_publishaudit")
    @ResponseBody
    public AjaxResult publishAudit(Long id, @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date publishTime, Integer state, String remark) {
        AjaxResult result = null;
        try {
            bidRequestService.publishAudit(id, publishTime, state, remark);
            result = new AjaxResult();
        } catch (DisplayableExcetion e) {
            e.printStackTrace();
            result = new AjaxResult(false, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            result = new AjaxResult(false, "系统出错,请联系管理员");
        }
        return result;
    }

    //查看借款审核页面
    @RequestMapping("/borrow_info")
    public String borrowInfo(Model model,Long id) {
        BidRequest bidRequest = bidRequestService.get(id);
        if (bidRequest != null) {
            model.addAttribute("bidRequest",bidRequest);
            UserInfo userInfo = userInfoService.get(bidRequest.getCreateUser().getId());
            model.addAttribute("userInfo",userInfo);
            model.addAttribute("realAuth",realAuthService.get(userInfo.getRealAuthId()));
            List<BidRequestAuditHistory> list = bidRequestAuditHistoryService.queryListByBidRequestId(bidRequest.getId());
            model.addAttribute("audits",bidRequestAuditHistoryService.queryListByBidRequestId(bidRequest.getId()));
        }
        return "/bidrequest/borrow_info";
    }

    //满标一审
    @RequestMapping("/bidrequest_audit1_list")
    public String bidrequestAudit1List(Model model,BidRequestQueryObject qo) {
        qo.setBidRequestState(Constants.BIDREQUEST_STATE_APPROVE_PENDING_1);
        model.addAttribute("pageInfo",bidRequestService.queryPage(qo));
        return "bidrequest/audit1";
    }

    //满标一审
    @RequestMapping("/bidrequest_audit1")
    @ResponseBody
    public AjaxResult audit(Long id,Integer state,String remark){
        AjaxResult result = null;
        try {
            bidRequestService.audit(id,state,remark);
            result = new AjaxResult();
        } catch (DisplayableExcetion e) {
            e.printStackTrace();
            result = new AjaxResult(false, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            result = new AjaxResult(false, "系统出错,请联系管理员");
        }
        return result;
    }

    //满标二审
    @RequestMapping("/bidrequest_audit2_list")
    public String bidrequestAudit2List(Model model,BidRequestQueryObject qo) {
        qo.setBidRequestState(Constants.BIDREQUEST_STATE_APPROVE_PENDING_2);
        model.addAttribute("pageInfo",bidRequestService.queryPage(qo));
        return "bidrequest/audit2";
    }
    //满标二审
    @RequestMapping("/bidrequest_audit2")
    @ResponseBody
    public AjaxResult audit2(Long id,Integer state,String remark){
        AjaxResult result = null;
        try {
            bidRequestService.audit2(id,state,remark);
            result = new AjaxResult();
        } catch (DisplayableExcetion e) {
            e.printStackTrace();
            result = new AjaxResult(false, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            result = new AjaxResult(false, "系统出错,请联系管理员");
        }
        return result;
    }

}
